IPixel.Utility = function(game) {
    this.game = game;
};
/**
 * scale image to fill gameworld fully.
 * @param  {Phaser.Image} sprite scalable object;
 */
IPixel.Utility.prototype.scaleToFullScreen = function(sprite) {
    var gameW = this.game.width,
        gameH = this.game.height,
        width = sprite.width,
        height = sprite.height,
        fac = this._getScaleFactor(gameW, gameH, width, height);

    sprite.scale.setTo(fac.x, fac.y);
};
/**
 * calculate scale factor
 * @param  {number} parentWidth  width of parent
 * @param  {number} parentHeight height of parent
 * @param  {number} childWidth   width of child
 * @param  {number} childHeight  height of parent
 * @return {object}              an object include factor of x axis and y axis.
 */
IPixel.Utility.prototype._getScaleFactor = function(parentWidth, parentHeight, childWidth, childHeight) {
    return {
        x: parentWidth / childWidth,
        y: parentHeight / childHeight
    };
};
/**
 * return a scaleConfig of NO_BORDER
 * @return {object} object has four properties factor,deltaX,deltaY
 */
IPixel.Utility.prototype._getNoBorderScaleFactor = function() {
    var gameW = this.game.world.width,
        gameH = this.game.world.height,
        cWidth = document.body.clientWidth,
        cHeight = document.body.clientHeight,
        fac = this._getScaleFactor(cWidth, cHeight, gameW, gameH);
    
    var scaleConfig = {
        factor: 1,
        deltaX: 0,
        deltaY: 0
    };
    scaleConfig.factor = Math.max(fac.x, fac.y);
    scaleConfig.deltaX = (gameW - cWidth / scaleConfig.factor);
    scaleConfig.deltaY = (gameH - cHeight / scaleConfig.factor);
    this.scaleConfig=scaleConfig;
    return scaleConfig;
};
IPixel.Utility.prototype.centerCamera = function() {
    var game=this.game;
    game.camera.x=(game.world.width-game.width)/2;
    game.camera.y=(game.world.height-game.height)/2;
};
/**
 * make your game scale like no_border strategy
 * @param  {number} desktopScale default scale factor,only use it for desktop.
 */
IPixel.Utility.prototype.noBorder=function(){
    var game=this.game;
    game.scaleConfig=this._getNoBorderScaleFactor();
    if(game.device.desktop){
        game.scale.scaleMode=Phaser.ScaleManager.SHOW_ALL;
    }else{
        game.scale.scaleMode=Phaser.ScaleManager.USER_SCALE;
        
        game.scale.setUserScale(this.scaleConfig.factor,this.scaleConfig.factor);
        
        game.world.setBounds(0, 0,this.game.world.width,this.game.world.height);
        
        game.scale.setGameSize(this.game.world.width-this.scaleConfig.deltaX,this.game.world.height-this.scaleConfig.deltaY);
        
        
    }
    //
    // window.addEventListener('resize', this.noBorder.bind(this));
};

/**
 * return a time counter.
 * @param  {Date} forward  
 * @param  {Date} backward 
 * @return {TimeCounter}       
 */
 IPixel.Utility.prototype.getTimeCounter = function(forward, backward) {
    //ms ---> s
    var delta = (forward.getTime() - backward.getTime()) / 1000;

    function TimeCounter(time) {
        if (time < 0 || typeof time === 'undefined') {
            time = 0;
        }
        this.delta = time;
    }
    TimeCounter.prototype.getDays = function() {
        return Math.floor(this.delta / (24 * 60 * 60));
    };
    TimeCounter.prototype.getHours = function() {
        return Math.floor(this.delta % (24 * 60 * 60) / 60 / 60);
    };
    TimeCounter.prototype.getMinutes = function() {
        return Math.floor(this.delta % (24 * 60 * 60) % (60 * 60) / 60);
    };
    TimeCounter.prototype.getSeconds = function() {
        return Math.floor(this.delta % (24 * 60 * 60) % (60 * 60) % 60);
    };
    TimeCounter.prototype.countDown = function(time) {
        if (time <= 0 || typeof time === 'undefined') {
            return;
        }
        if (this.delta >= 0) {
            this.delta -= time / 1000;
            if (this.delta < 0) {
                this.delta = 0;
            }
        }
    };
    TimeCounter.prototype.isTimeOver = function() {
        if (this.delta === 0) {
            return true;
        }
        return false;
    };
    return new TimeCounter(delta);
};
/*\
|*|
|*|  :: cookies.js ::
|*|
|*|  A complete cookies reader/writer framework with full unicode support.
|*|
|*|  https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|*|
|*|  This framework is released under the GNU Public License, version 3 or later.
|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*|  Syntaxes:
|*|
|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*|  * docCookies.getItem(name)
|*|  * docCookies.removeItem(name[, path], domain)
|*|  * docCookies.hasItem(name)
|*|  * docCookies.keys()
|*|
\*/
IPixel.Utility.prototype.docCookies={
    getItem : function(sKey) {
            return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
        },
        setItem : function(sKey, sValue, vEnd, sPath, sDomain, bSecure) {
            if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) {
                return false;
            }
            var sExpires = "";
            if (vEnd) {
                switch (vEnd.constructor) {
                    case Number:
                        sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
                        break;
                    case String:
                        sExpires = "; expires=" + vEnd;
                        break;
                    case Date:
                        sExpires = "; expires=" + vEnd.toUTCString();
                        break;
                }
            }
            document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
            return true;
        },
        removeItem : function(sKey, sPath, sDomain) {
            if (!sKey || !this.hasItem(sKey)) {
                return false;
            }
            document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
            return true;
        },
        hasItem : function(sKey) {
            return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
        },
        keys: /* optional method: you can safely remove it! */ function() {
            var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
            for (var nIdx = 0; nIdx < aKeys.length; nIdx++) {
                aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
            }
            return aKeys;
        }
};
